﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Entities;

public partial class Restaruant_Restaurant : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();
        }
    }

    protected void InitData()
    {
        //Freeze();

        // Init controls' data
        restaurant_district_dropdownlist.DataSource = BLL.Command.GetDistricts(BLL.Command.DistrictType.ForRestaurant);
        restaurant_district_dropdownlist.DataBind();
        served_district_checkboxlist.DataSource = BLL.Command.GetDistricts(BLL.Command.DistrictType.ForConsumer);
        served_district_checkboxlist.DataBind();

        Int16 restaurantId = 0;
        String queryString = Request.QueryString["restaurant_id"];
        if (Int16.TryParse(queryString, out restaurantId))
            Session["restaurant_id"] = restaurantId;
        else if (Session["restaurant_id"] != null)
            Int16.TryParse(Session["restaurant_id"].ToString(), out restaurantId);

        if (restaurantId == 0)
            return;

        Restaurant restaurant = BLL.Command.GetRestaurant(restaurantId);
        if (null == restaurant)
            return;

        ///
        // Display the restaurant info
        ///
        restaurant_id_label.Text = restaurant.Id.ToString() ;
        name_textbox.Text = restaurant.Name;
        tel_textbox.Text = restaurant.Tel;
        mobile_textbox.Text = restaurant.Mobile;
        address_textbox.Text = restaurant.Address.Description;
        delivery_id_label.Text = restaurant.Delivery.Id.ToString();
        delivery_policy_textbox.Text = restaurant.Delivery.Description;
        advertise_textbox.Text = restaurant.Advertise;
        start_time_textbox.Text = restaurant.Begin;
        close_time_textbox.Text = restaurant.End;
        state_dropdownlist.Items.FindByValue(restaurant.Open.ToString()).Selected = true;
        restaurant_district_dropdownlist.SelectedValue = restaurant.Address.District.Id.ToString();

        served_district_checkboxlist.DataSource = BLL.Command.GetDistricts(BLL.Command.DistrictType.ForConsumer);
        served_district_checkboxlist.DataBind();

        List<Entities.District> servedDistricts = BLL.Command.GetServedDistricts(restaurantId);

        for (int i=servedDistricts.Count-1; i>=0; i--)
        {
            Int16 districtId = servedDistricts[i].Id;
            for (int j = 0; j < served_district_checkboxlist.Items.Count; j++)
            {
                Int16 districtId2 = Convert.ToInt16(served_district_checkboxlist.Items[j].Value);
                if (districtId == districtId2)
                {
                    served_district_checkboxlist.Items[j].Selected = true;
                }
            }
        }
    }
    protected void UpdateNotice(String notice, bool isWarning)
    {
        notice_lable.Text = notice;

        if (isWarning)
            notice_lable.ForeColor = System.Drawing.Color.Red;
    }
    //protected void Freeze()
    //{
    //    name_textbox.ReadOnly = true;
    //    restaurant_district_dropdownlist.Enabled = false;
    //    address_textbox.ReadOnly = true;
    //    tel_textbox.ReadOnly = true;
    //    mobile_textbox.ReadOnly = true;
    //    start_time_textbox.ReadOnly = true;
    //    close_time_textbox.ReadOnly = true;
    //    served_district_checkboxlist.Enabled = false;
    //    state_dropdownlist.Enabled = false;
    //    advertise_textbox.ReadOnly = true;

    //    edit_button.Enabled = true;
    //    save_button.Enabled = false;
    //}
    //protected void Edit() 
    //{
    //    name_textbox.ReadOnly = false;
    //    restaurant_district_dropdownlist.Enabled = true;
    //    address_textbox.ReadOnly = false;
    //    tel_textbox.ReadOnly = false;
    //    mobile_textbox.ReadOnly = false;
    //    start_time_textbox.ReadOnly = false;
    //    close_time_textbox.ReadOnly = false;
    //    served_district_checkboxlist.Enabled = true;
    //    state_dropdownlist.Enabled = true;
    //    advertise_textbox.ReadOnly = false;

    //    edit_button.Enabled = false;
    //    save_button.Enabled = true;

    //    name_textbox.Focus();
    //}
    protected void OnSubmitBtnClick(object sender, EventArgs e)
    {
        //Freeze();
        Restaurant restaurant = new Restaurant();

        Int16 restaurantId = 0;
        String queryString = Request.QueryString["restaurant_id"];
        if (!Int16.TryParse(restaurant_id_label.Text.Trim(), out restaurantId)
            || restaurantId == 0)
        {
            restaurantId = 0;

            // New Restruant
            Int16 ownerId = 0;
            Int16.TryParse(Session["owner_id"].ToString(), out ownerId);
            restaurant.Owner = new Owner();
            restaurant.Owner.Id = ownerId;
        }
        restaurant.Id = restaurantId;
        restaurant.Name = name_textbox.Text.Trim();
        restaurant.Tel = tel_textbox.Text.Trim();
        restaurant.Mobile = mobile_textbox.Text.Trim();
        restaurant.Advertise = advertise_textbox.Text.Trim();
        restaurant.Begin = start_time_textbox.Text.Trim();
        restaurant.End = close_time_textbox.Text.Trim();
        restaurant.Open = Convert.ToBoolean(state_dropdownlist.SelectedValue.Trim());

        restaurant.Address = new Address();
        restaurant.Address.Description = address_textbox.Text.Trim();
        restaurant.Address.District = new District(Convert.ToInt16(restaurant_district_dropdownlist.Text.Trim()));

        restaurant.Delivery = new Delivery();
        Int16.TryParse(delivery_id_label.Text.Trim(), out restaurant.Delivery.Id);
        restaurant.Delivery.Description = delivery_policy_textbox.Text.Trim();

        restaurant.Score = new Score();

        ///
        // Collect served district
        List<District> servedDistricts = new List<District>();
        for (int i = 0; i < served_district_checkboxlist.Items.Count; i++)
        {
            if (served_district_checkboxlist.Items[i].Selected)
                servedDistricts.Add(new District(Convert.ToInt16(served_district_checkboxlist.Items[i].Value)));
        }
        restaurant.ServedDistricts = servedDistricts;

        if (restaurant.Id == 0)
        {
            if (BLL.Command.NewRestaurant(ref restaurant))
            {
                Session["restaurant_id"] = restaurant.Id;
                UpdateNotice("新餐厅创建成功!", false);
            }
            else
                UpdateNotice("新餐厅创建失败，请检查您输入的信息，或者联系网站管理员!!!", true);
        }
        else
        {
            if (BLL.Command.UpdateRestaurant(ref restaurant))
            {
                UpdateNotice("餐厅信息更新成功!", false);
            }
            else
                UpdateNotice("餐厅信息更新失败，请检查您输入的信息，或者联系网站管理员!!!", true);
        }
    }
    protected void OnEditBtnClick(object sender, EventArgs e)
    {
        //Edit();
    }
}
